/*
package com.example.message.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

@Component
public class ChatHandler implements WebSocketHandler {

    private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("Connection established: " + session.getId());
        sessions.add(session);
        broadcast("User " + session.getId() + " joined the chat");
    }

    @MessageMapping("/chat")
    public void handleChatMessage(Message message) {
        System.out.println("Received message: " + message);
        broadcast(message);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message)
            throws Exception {
        System.out.println("handleMessage11111111111");
        System.out.println("Received message: " + message);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception)
            throws Exception {
        System.out.println("Transport error: " + exception.getMessage());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
            throws Exception {
        System.out.println("Connection closed: " + session.getId() + " (" + closeStatus.getCode() + ")");
        sessions.remove(session);
        broadcast("User " + session.getId() + " left the chat");
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

    private void broadcast(Object message) {
        for (WebSocketSession session : sessions) {
            try {
                session.sendMessage(new TextMessage(new ObjectMapper().writeValueAsString(message)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
*/
